A type-passing implementation of value types and genericity

نویسنده

  • Timothy Paul Owen
چکیده

classes cannot be instantiated, but can be extended by any kind of class. They can extend only other abstract classes. There is a distinguished abstract class called Object which is the ultimate superclass of any other class. A class containing abstract methods, either declared or inherited, must itself be declared abstract. Reference classes are similar to traditional Java classes: their instances are allocated on the heap, and are manipulated indirectly via pointers. Objects of a reference class can be aliased, as there may be several pointers to the same object. The superclass of a reference class must be either another reference class or an abstract class. Value class instances are manipulated directly and cannot be aliased. A value class can extend only abstract classes or other value classes. Value classes are the only kind of value types in Pluto — Chapter 4 gives more detail on these, including some restrictions on subclasses of value classes. An object whose type is a value class is immutable: its instance fields cannot be updated once created. Section 4.1.2 explains the rationale for this decision. Reference and value classes may be declared final, to indicate that they cannot have subclasses. The arrangement of the different kinds in the class hierarchy is shown in Figure 3.1. The restriction on abstract classes extending only other abstract classes keeps the type system simpler, but it could be loosened to allow abstract classes to extend value and reference classes. However, the type system would then need to ensure that a reference class does not extend a value class indirectly via an Chapter 3. The basic Pluto language and compiler 33 intermediate abstract class. Otherwise there could be confusing behaviour caused by the different semantics of value-based and reference-based types. See Chapter 4 for details about the support for value-based types in Pluto. • Fields, value parameters and local variables always refer to a distinct object for reference types — there is no null or missing value. Local variables can be declared mutable, but cannot be used until they have been assigned a value at least once. • Classes, interfaces and methods support multiple type parameters, which can be bounded. Chapter 5 covers the design and implementation of genericity in more detail. • In the class hierarchy, method signatures can be varied when overridden: return types can be specialised and parameters can be generalised, in accordance with the variance rules covered in Section 2.7. This ability is not supported for multiple inheritance of method signatures through interface types. Whenever a method of some name is inherited through more than one supertype, the signatures must match exactly. The reason for this restriction is covered in Section 6.4.2. Figure 3.2 lists a simple but complete Pluto program. The point class shows the use of symbolic method names. It defines a method called == with one parameter, and a method called $ with no parameters. These methods can be called using infix and prefix expressions, respectively. Given a point object, say p, the prefix expression $p invokes the $ method of the p object. Similarly, an infix expression such as p == p invokes the == method of the p object, with itself as the single argument. The code within the methods of the point class also shows use of this syntax, as it invokes the == and $ methods that are defined by the int class. The transcript in Figure 3.3 shows a typical build session, where the Pluto source in file example.pto is compiled into C code by the first command, then the C compiler and linker is invoked to build an executable. 3.3 Compiler structure The overall design of the Pluto compiler is shown in Figure 3.4, which lists the different data structures used at each stage of compilation, together with the transformations applied at each pass. 3.3.1 Implementation language The Pluto compiler is written in Java[49], which proved useful in the development of a complex piece of software such as a compiler. For the purpose of this experimental work, speed of execution was not especially important compared to correctness and ease of development. The addition of genericity to Java (in particular for collection data structures) would have improved the clarity of the compiler code, and reduced development time. It would be straightforward to rewrite the compiler in the Pluto language itself, provided some form of file reading and writing capability were added to the standard library. The Java 2 Platform standard libraries contain a rich set of classes for building a graphical user interface, such as the AWT and Swing packages. In particular, the Swing JTree Chapter 3. The basic Pluto language and compiler 34 final value class point(x:int, y:int) { field x : int = x; field y : int = y; method ==(other:point) : boolean { return this.x == other.x && this.y == other.y; } method $() : String { return “[” + $this.x + “,” + $this.y + “]”; } static field origin : point = new point(0, 0); } abstract class Shape(pos:point) { field position : point = pos; abstract method draw(); }class Shape(pos:point) { field position : point = pos; abstract method draw(); } class Circle(centre:point, rad:int) extends Shape(centre) { field radius : int = rad; method draw() { String.println(“A circle at ” + $this.position + “ with radius: ” + $this.radius); } } abstract class Main() { static method run() : int { let s : Shape = new Circle(point.origin, 4); s.draw(); return 0; } }class Main() { static method run() : int { let s : Shape = new Circle(point.origin, 4); s.draw(); return 0; } } Figure 3.2: Example Pluto program % java pluto.Main example.pto % gcc pto_src*.c -lpluto -o example % ./example A circle at [0,0] with radius: 4 Figure 3.3: Example build process and output Chapter 3. The basic Pluto language and compiler 35 component allows a tree or graph structure to be examined using an intuitive graphical browser. It proved simple to add the necessary code to the Pluto compiler so that the abstract syntax tree, core tree and C level intermediate representation could be viewed using JTrees. This facility was of great use in locating errors in the compiler itself, by visual inspection of the intermediate representation of each stage.

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

منابع مشابه

Analytical and Experimental Investigation of I Beam-to-CFT Column Connections under Monotonic Loading (RESEARCH NOTE)

In this study, the behavior characteristics of I beam-to-concrete filled tube (CFT) column connection is studied through experiment and finite element models under the monotonic loading. To validate the finite element modeling, at first, an experimental model is made and experimented. After validation of the finite element modeling, different models were created in the software. The studied par...

متن کامل

Genericity through Constrained Types

Modern object-oriented languages such as X10 require a rich framework for types capable of expressing valuedependency, type-dependency and supporting pluggable, application-specific extensions. In earlier work, we presented the framework of constrained types for concurrent, object-oriented languages, parametrized by an underlying constraint system C . Constraint systems are a very expressive fr...

متن کامل

Semantics-Driven Genericity: A Sequel to the Static C++ Object-Oriented Programming Paradigm (SCOOP 2)

Classical (unbounded) genericity in C++03 defines the interactions between generic data types and algorithms in terms of concepts. Concepts define the requirements over a type (or a parameter) by expressing constraints on its methods and dependent types (typedefs). The upcoming C++0x standard will promote concepts from abstract entities (not directly enforced by the tools) to language construct...

متن کامل

Genericity in Java with Virtual Types

This paper suggests virtual types for Java, a language mechanism which subsumes parameterized classes, while also integrating more naturally with Java’s object model. The same basic mechanism is also known as virtual patterns in Beta and as generics in Ada95. We discuss various issues in the Java type system, issues with inheritance and genericity in general, and give a specific suggestion as t...

متن کامل

Study on effect of detector type in estimating buildup factor of gamma-rays by monte carlo simulation based on variance reduction

This paper presents the study on the effect of detector type in estimating gamma rays buildup factor. In this regard, the flux buildup factors of gamma rays emitted by an isotropic point 60Co source in mean energy 1.253 MeV was evaluated after passing through the Pb, W, Zn, and Ti sample in thickness of 1-5 cm by MCNPX. Four common detector types in simulation of measurement was defined to esti...

متن کامل

Influence of environmental conditions on the earth pit resistance using earth pit simulator

Introduction: Electrocution is the cause of many deaths in the world annually, that electric current passing between the body and earth can be the reason; thus, it is of great importance to construct the ground protection system using earth pit. This study aimed to investigate the effective environmental conditions on the earthchr('39')s pit resistance using the simulation method. Materials an...

متن کامل

ذخیره در منابع من


  با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

عنوان ژورنال:

دوره   شماره 

صفحات  -

تاریخ انتشار 2002